package org.example.com.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.com.Bean.Book;
import org.example.com.mapper.BookMapper;
import org.example.com.service.Bookservice;
import org.example.com.util.JsonResult;
import org.example.com.util.RecultTool;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceimpl extends ServiceImpl<BookMapper, Book> implements Bookservice {
    @Override
    public JsonResult findFreshBook() {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("limit 5");
        List<Book> list=list(queryWrapper);
        return RecultTool.success(list);
    }
    @Override
    public JsonResult findLowerBook() {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("price");
        queryWrapper.last("limit 4");
        List<Book> list=list(queryWrapper);
        return RecultTool.success(list);
    }
    @Override
    public JsonResult findFreshBookmore(int page) {
        Page<Book> of=Page.of(page,10);
        QueryWrapper<Book> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        List<Book> list=list(of,queryWrapper);
        of.setRecords(list);
        return RecultTool.success(of);
    }
    @Override
    public JsonResult findLowerBookmore(int page) {
        Page<Book> of=Page.of(page,10);
        QueryWrapper<Book> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByAsc("price");
        List<Book> list=list(of,queryWrapper);
        of.setRecords(list);
        return RecultTool.success(of);
    }
    @Override
    public JsonResult findSearchByBookName(String name, int page) {
       Page<Book> of=Page.of(page,10);
       QueryWrapper<Book> queryWrapper=new QueryWrapper<>();
       queryWrapper.like("name",name);
       List<Book> list=list(of,queryWrapper);
       of.setRecords(list);
       return RecultTool.success(of);
    }
    @Override
    public JsonResult findCategoryBook(String categoryId, int page) {
       Page<Book> of=Page.of(page,10);
       QueryWrapper<Book> queryWrapper=new QueryWrapper<>();
       queryWrapper.eq("category_id",categoryId);
       List<Book> list=list(of,queryWrapper);
       of.setRecords(list);
       return RecultTool.success(of);
    }
}
